﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000281 RID: 641
	internal class DefaultIfEmptyObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000E64 RID: 3684 RVA: 0x00043AF0 File Offset: 0x00041EF0
		public DefaultIfEmptyObservable(IObservable<T> source, T defaultValue) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.defaultValue = defaultValue;
		}

		// Token: 0x06000E65 RID: 3685 RVA: 0x00043B0C File Offset: 0x00041F0C
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			//return this.source.Subscribe(new DefaultIfEmptyObservable<IObservable<T>, T>DefaultIfEmpty(this, observer, cancel));
			return null;
		}

		// Token: 0x0400084D RID: 2125
		private readonly IObservable<T> source;

		// Token: 0x0400084E RID: 2126
		private readonly T defaultValue;

		// Token: 0x02000282 RID: 642
		private class DefaultIfEmpty : OperatorObserverBase<T, T>
		{
			// Token: 0x06000E66 RID: 3686 RVA: 0x00043B21 File Offset: 0x00041F21
			public DefaultIfEmpty(DefaultIfEmptyObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.hasValue = false;
			}

			// Token: 0x06000E67 RID: 3687 RVA: 0x00043B39 File Offset: 0x00041F39
			public override void OnNext(T value)
			{
				this.hasValue = true;
				this.observer.OnNext(value);
			}

			// Token: 0x06000E68 RID: 3688 RVA: 0x00043B50 File Offset: 0x00041F50
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000E69 RID: 3689 RVA: 0x00043B88 File Offset: 0x00041F88
			public override void OnCompleted()
			{
				if (!this.hasValue)
				{
					this.observer.OnNext(this.parent.defaultValue);
				}
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400084F RID: 2127
			private readonly DefaultIfEmptyObservable<T> parent;

			// Token: 0x04000850 RID: 2128
			private bool hasValue;
		}
	}
}
